package vn.gq.udv.db.items;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class ChapterItem implements Parcelable {
	private int id, catId, position = 0;
	private String title;
	private int scrollY = 0;
	
	
	public ChapterItem() {
	}
	
	public ChapterItem(String json) throws JSONException {
		JSONObject in = new JSONObject(json);
		id = in.getInt("id");
		catId = in.getInt("catId");
		title = in.getString("title");
		position = in.getInt("position");
		scrollY = in.getInt("scrollY");
	}
	
	public ChapterItem(Parcel in) {
		id = in.readInt();
		catId = in.readInt();
		title = in.readString();
		position = in.readInt();
		scrollY = in.readInt();
	}
	
	public void setScrollYPos(int val) {
		scrollY = val;
	}
	
	public int getScrollY() {
		return scrollY;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public int getCatId() {
		return catId;
	}
	public void setCatId(int catId) {
		this.catId = catId;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public int describeContents() {
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(catId);
		dest.writeString(title);
		dest.writeInt(position);
		dest.writeInt(scrollY);
	}
	
	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public static final Parcelable.Creator<ChapterItem> CREATOR = new Parcelable.Creator<ChapterItem>() {
		
		@Override
		public ChapterItem[] newArray(int size) {
			return new ChapterItem[size];
		}
		
		@Override
		public ChapterItem createFromParcel(Parcel source) {
			return new ChapterItem(source);
		}
	};
	
	public JSONObject toJSonObject() {
		JSONObject obj = new JSONObject();
		try {
			obj.put("id", id);
			obj.put("catId", catId);
			obj.put("title", title);
			obj.put("position", position);
			obj.put("scrollY", scrollY);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return obj;
	}
}
